
+function ($) {
     'use strict'; 
     
	 var kui =$.kui = window.kui = window.kui || {};
	 
	 kui.version='V1.0';
	 
	 kui.namespace='kui'; 
	 
	 kui.kuiExtend =function(me,parent, option)
	 {
		if (typeof parent != 'function') return me;
		 //保存对父类的引用
		me.base = parent.prototype;
		me.base.constructor = parent;
		//继承
		var f = function () { }; 
		$.extend(f.prototype,parent.prototype);
		if (option) $.extend(true,f.prototype, option);
		  
		$.extend(true,me.prototype, f.prototype); 
	 };
    
	function kuibaseExtend(me,parent, option)
	 {
		if (typeof parent != 'function') return me;
		//保存对父类的引用
		me.base = parent.prototype;
		me.base.constructor = parent;
		//继承
		var f = function () { };
		f.prototype = parent.prototype;
		me.prototype = new f();
		me.prototype.constructor = me;
		//附加属性方法
		if (option) $.extend(true,me.prototype, option);
	 };
	   
		
	$.widget = function( name, base, option ) {
		if ( !option ) {
			option ={};
		}
		if ( !base ) {
			base = kui.UIComponent;
		}
	    if(!$.kui[ name ]){
			$.kui[ name ]= function( option, element ) { 
				$.kui[ name ].base.constructor.call(this,option,element);
				if ( arguments.length ) {
					this._createWidget( option, element );
				}
			};
		}
		
		$.kui[ name ].base = base.prototype;
		$.kui[ name ].base.Constructor = base; 
		   
		var basePrototype = new base();  
		basePrototype.options = $.extend( true, {}, basePrototype.options ); 
		
		var f = function () { }; 
		$.extend(true,f.prototype,basePrototype,$.kui[ name ].prototype);
		$.extend(true,$.kui[ name ].prototype,f.prototype,{ 
			ctrName: name
		} , option );
		 
		$.bridge( name,$.kui[ name ]);  
		 
	};
	
	$.bridge = function( name, object ) {
		$.fn[ name ] = function( option ) {
			
			var isMethodCall = typeof option === "string";
			var args = Array.prototype.slice.call( arguments, 1 );
			
			var	returnValue = this; 
		 
			option = !isMethodCall && args.length ? $.extend.apply( null, [ true, option ].concat(args) ) : option;
	 
			if ( isMethodCall && option.charAt( 0 ) === "_" ) {
				return returnValue;
			}
			
			var defaults = $.kui[ name ].DEFAULTS;
			  
			return this.each(function () {
				var $this   = $(this)  
				var data    = $this.data(name)
				 
				if (!data) 
				{
					var toptions = $.extend(true,{},defaults,$this.options,$this.data(), typeof option == 'object' && option);
	                 $this.data(name, data = new object(toptions,$this) );  
				 
					if (typeof option == 'string' && $.isFunction(data[option])) { 
						if (!args) data[option]();
						else data[option].apply(data, args);
					}
				}else{ 
				   
					if (typeof option == 'string' && $.isFunction(data[option])) { 
						if (!args) data[option]()
						else data[option].apply(data, args)
					} else {
						var toptions = $.extend(true,{},$this.options,$this.data(), typeof option == 'object' && option);
						data.set( toptions || {} ).init();
					}
				}
			})
			 return returnValue;
		};
		$.kui[ name ].Constructor  = object;
	};

 
    kui.Component = function (option)
    {  
	    this.events = this.events || {};
		this.options= this.options || {};
        this.set(option || {}); 
    };
    $.extend(kui.Component.prototype, { 
		namespace: kui.namespace, 
		ctrName: "Component",  
		DEFAULTS :{}, 
        getType: function ()
        {
            return 'kui.Component';
        },   
        //设置属性
        // arg 属性名    value 属性值 
        // arg 属性/值   value 是否只设置事件
        set: function (arg, value)
        {
            if (!arg) return;
            if (typeof arg == 'object')
            {
                var tmp;
                if (this.options != arg)
                {
                    $.extend(this.options, arg);
                    tmp = arg;
                }
                else
                {
                    tmp = $.extend({}, arg);
                }
                if (value == undefined || value == true)
                {
                    for (var p in tmp)
                    {
                        if (p.indexOf('on') == 0)
                            this.set(p, tmp[p]);
                    }
                }
                if (value == undefined || value == false)
                {
                    for (var p in tmp)
                    {
                        if (p.indexOf('on') != 0)
                            this.set(p, tmp[p]);
                    }
                }
                return this;
            }
            var name = arg;
            //事件参数
            if (name.indexOf('on') == 0)
            {
                if (typeof value == 'function')
                    this.bind(name.substr(2), value);
                return;
            }
            if (!this.options) this.options = {};
			
            if (this.trigger('propertychange', [arg, value]) == false) return;
			
            this.options[name] = value;
			
            var pn = '_set' + name.substr(0, 1).toUpperCase() + name.substr(1);
            if (this[pn])
            {
                this[pn].call(this, value);
            }
			
            this.trigger('propertychanged', [arg, value]);
			
			return this;
        },

        //获取属性
        get: function (name)
        {
            var pn = '_get' + name.substr(0, 1).toUpperCase() + name.substr(1);
            if (this[pn])
            {
                return this[pn].call(this, name);
            }
            return this.options[name];
        },

        hasBind: function (arg)
        {
            var name = arg.toLowerCase();
            var event = this.events[name];
            if (event && event.length) return true;
            return false;
        },

        //触发事件
        //data (可选) Array(可选)传递给事件处理函数的附加参数
        trigger: function (arg, data)
        {
            if (!arg) return;
            var name = arg.toLowerCase();
            var event = this.events[name];
            if (!event) return;
            data = data || [];
            if ((data instanceof Array) == false)
            {
                data = [data];
            }
            for (var i = 0; i < event.length; i++)
            {
                var ev = event[i];
                if (ev.handler.apply(ev.context, data) == false)
                    return false;
            }
        },

        //绑定事件
        bind: function (arg, handler, context)
        {
            if (typeof arg == 'object')
            {
                for (var p in arg)
                {
                    this.bind(p, arg[p]);
                }
                return;
            }
            if (typeof handler != 'function') return false;
            var name = arg.toLowerCase();
            var event = this.events[name] || [];
            context = context || this;
            event.push({ handler: handler, context: context });
            this.events[name] = event;
        },
         one: function (arg, handler, context)
        {
            if (typeof arg == 'object')
            {
                for (var p in arg)
                {
                    this.one(p, arg[p]);
                }
                return;
            }
            if (typeof handler != 'function') return false;
            var name = arg.toLowerCase();
            var event = this.events[name] || [];
            context = context || this;
			var  that =this;
			var original = handler;
			handler = function() {
				that.unbind(name, handler);
				original.apply(that, arguments);
			};      
            event.push({ handler: handler, context: context });
            this.events[name] = event;
        },
        //取消绑定
        unbind: function (arg, handler)
        {
            if (!arg)
            {
                this.events = {};
                return;
            }
            var name = arg.toLowerCase();
            var event = this.events[name];
            if (!event || !event.length) return;
            if (!handler)
            {
                delete this.events[name];
            }
            else
            {
                for (var i = 0, l = event.length; i < l; i++)
                {
                    if (event[i].handler == handler)
                    {
                        event.splice(i, 1);
                        break;
                    }
                }
            }
        }, 
		destroy: function() {  
		    this.unbind();
			this.options = null;
		}  
		
    });
 
	kui.UIComponent = function( option, element ) {  
		kui.UIComponent.base.constructor.call(this,option);
		if ( arguments.length ) {
			this._createWidget(element );
		}
	};

	kui.kuiExtend(kui.UIComponent,kui.Component, {  
		ctrName: "UIComponent",  
		getType: function ()
		{
			return 'kui.UIComponent';
		},
		_createWidget: function( element ) { 
			this.element = element;  
			this._create();
			this.trigger("create");
			this.init();
		}, 
		_create: function() {},
		
		init: function() {}, 
		
		setClass: function (value)
		{
			if (this.element && value)
			{
				$(this.element).addClass(value);
			}
		} , 
		destroy: function() { 
			var that = this; 
			kui.UIComponent.base.destroy.call(that);
			that.element.removeData(that.ctrName); 
			if (that.element)
			{
				that.element.remove();
			} 
		}  
    }); 
	
    $.extend(kui, { 
        ISDEBUG   : false,
        KeyPressed : { //key press state
            ctrl  : false,
            shift : false
        },
        keyCode: {
            ENTER : 13, ESC  : 27, END : 35, HOME : 36,
            SHIFT : 16, CTRL : 17, TAB : 9,
            LEFT  : 37, RIGHT: 39, UP  : 38, DOWN : 40,
            DELETE: 46, BACKSPACE: 8
        },
        eventType: {
			initDomUI         : 'bjui.initDomUI',         // When document load completed or ajax load completed, B-JUI && Plugins init 
            beforeInitDomUI   : 'bjui.beforeInitDomUI',   // If your DOM do not init [add to DOM attribute 'data-noinit="true"']
            afterInitDomUI    : 'bjui.afterInitDomUI',    // 
            ajaxStart      : 'bjui.ajaxStart', 
            ajaxStatus     : 'bjui.ajaxStatus',     // When performing ajax request, display or hidden progress bar
            ajaxStop      : 'bjui.ajaxStop', 
            ajaxError : 'bjui.ajaxError', 
            beforeCloseNavtab : 'bjui.beforeCloseNavtab',
            beforeCloseDialog : 'bjui.beforeCloseDialog',
            afterCloseNavtab  : 'bjui.afterCloseNavtab',
            afterCloseDialog  : 'bjui.afterCloseDialog',
            initLayout : 'bjui.initLayout'
        }, 
		config: {ok:200, error:300, timeout:301,ajaxTimeout: 30000,alertTimeout: 6000},  
        debug: function(msg) {
            if (this.ISDEBUG) {
                if (typeof(console) != 'undefined') console.log(msg)
                else alert(msg)
            }
        },
        init: function(options) {
            var op = $.extend({}, options)  
            $.extend(kui.config, op.config);
            this.ISDEBUG = op.ISDEBUG || false;
        }, 
        regional: {},
        setRegional: function(key, value) {
            kui.regional[key] = value
        },
        getRegional : function(key) {
            if (String(key).indexOf('.') >= 0) {
                var msg, arr = String(key).split('.')
                
                for (var i = 0; i < arr.length; i++) {
                    if (!msg) msg = kui.regional[arr[i]]
                    else msg = msg[arr[i]]
                }
                
                return msg
            } else {
                return kui.regional[key]
            }
        },
        doRegional: function(frag, regional) {
            $.each(regional, function(k, v) {
                frag = frag.replaceAll('#'+ k +'#', v)
            }) 
            return frag
        }  
    }); 
    
}(jQuery);



